//
//  Core.m
//  My1A2B
//
//  Created by sun ives on 2009/12/29.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "Core.h"


@implementation Core

- (id)init{
	if (self == nil) {
		self = [super init];
	}
	m_answer = NULL;
	m_answerCount = 0;
	m_gameCount = 0;
	m_summary.count = 0;
	m_gameHead = m_gameCurr = NULL;
	m_newRecord = false;
	
	return self;
}

- (void)reStart:(int)count{
	time_t t;
	m_startTime = time(&t);
	m_answerCount = count;
	[self destroy1];
	m_answer = (char*)malloc(m_answerCount+1);
	memset(m_answer, '\0', m_answerCount+1);
	m_playTimeText = (char*)malloc(LIMIT);
	memset(m_playTimeText, '\0', LIMIT);
	m_gameCount++;
	m_guessCount = 1;
	m_playTime = m_playTimeMinute = m_playTimeSecond = 0;
	
	strGame *gameLast = m_gameHead;
	int no = 1;
	while (gameLast != NULL) {
		no = gameLast->no;
		gameLast = gameLast->next;
		no++;
	}
	struct tm *ts = localtime(&m_startTime);
	strGame *game = (strGame*)malloc(sizeof(strGame));
	memset(game, '\0', sizeof(strGame));
	game->no = no;
	game->startTime = (char*)malloc(LIMIT);
	memset(game->startTime, '\0', LIMIT);
	strftime(game->startTime, LIMIT, "%Y-%m-%d %H:%M:%S", ts);
	game->next = NULL;
	
	if (m_gameCurr != NULL){
		m_gameCurr->next = game;
	}else {
		m_gameHead = game;
	}
	m_gameCurr = game;
	m_newRecord = false;
	[self makeAnswer];	
}
- (void)makeAnswer{
	time_t t;
	srand((unsigned) time(&t));
	for (int i = 0; i < m_answerCount; i++) {
		int r = rand() % 10;
		if ([self checkDup:r]){
			i--;
			continue;
		}
		char a;
		sprintf(&a, "%d", r);
		*(m_answer+i) = a;
		
	}
}
- (bool)checkDup:(int)n{
	bool bResult = FALSE;
	for (int i = 0; i < m_answerCount; i++) {
		char m1 = *(m_answer+i);
		int m = atoi(&m1);
		if (n == m) {
			bResult = TRUE;
			break;
		}
	}
	return bResult;
}
- (char *)getAnswer{
	return m_answer;
}
- (int)getAnswerCount{
	return m_answerCount;
}
- (int)getGuessCount{
	return m_guessCount;
}
- (char *)getPlayTimeText{
	return m_playTimeText;
}
- (int)getPlayTimeMinute{
	return m_playTimeMinute;
}
- (int)getPlayTimeSecond{
	return m_playTimeSecond;
}
- (strGame *)getGameFromNO:(int)no{
	if (m_gameHead == NULL) {
		return NULL;
	}
	strGame *game = m_gameHead;
	BOOL BFind = NO;
	while (game != NULL) {
		int gameNo = game->no;
		if (gameNo == no) {
			BFind = YES;
			break;
		}
		game = game->next;
	}
	if (game && BFind) {
		return game;
	}else {
		return NULL;
	}

}

- (char *)compare:(char *)number{
	strGuess tempGuess;
	tempGuess.no = m_guessCount;
	memset(tempGuess.number, '\0', LIMIT);
	strcpy(tempGuess.number, number);
	char *chResult = (char *)malloc(LIMIT);
	memset(chResult, '\0', LIMIT);
	int A = 0;
	int B = 0;
	for (int i = 0; i < m_answerCount; i++){
		char g = *number++;
		char *temp_answer = (char *)malloc(m_answerCount+1);
		char *temp_answer1 = temp_answer;
		memset(temp_answer, '\0', m_answerCount+1);
		memcpy(temp_answer, m_answer, m_answerCount+1);
		for (int j = 0; j < m_answerCount; j++) {
			char a = *temp_answer++;
			if (g == a && i == j){
				A++;
				break;
			}else if (g == a && i != j) {
				B++;
				break;
			}
		}
		free(temp_answer1);
	}
	sprintf(chResult, "%dA%dB", A, B);
	memset(tempGuess.result, '\0', LIMIT);
	strcpy(tempGuess.result, chResult);
	m_gameCurr->guess[m_guessCount-1] = tempGuess;
	if (strcmp(m_answer, tempGuess.number) == 0) {
		time_t t;
		m_stopTime = time(&t);
		m_playTime = (int)(difftime(m_stopTime, m_startTime));
		[self formatTime:m_playTime];
		m_gameCurr->guessTime = (char*)malloc(LIMIT);
		memset(m_gameCurr->guessTime, '\0', LIMIT);
		strcpy(m_gameCurr->guessTime, m_playTimeText);
		m_gameCurr->count = m_guessCount;
		m_gameCurr->answer = (char*)malloc(LIMIT);
		memset(m_gameCurr->answer, '\0', LIMIT);
		strcpy(m_gameCurr->answer, m_answer);
		[self plusSummaryCount];
		m_newRecord = true;
	}
	m_guessCount++;	
	return chResult;
}
- (void)formatTime:(int)second{
	m_playTimeMinute = second/60;
	m_playTimeSecond = second - (60*m_playTimeMinute);
	sprintf(m_playTimeText, "%dM:%dS", m_playTimeMinute, m_playTimeSecond);

}

- (strGame *)setGame:(int)no count:(int)count answer:(char *)answer startTime:(char *)startTime guessTime:(char *)guessTime guess:(strGuess *)guess{
	strGame *tempGame = (strGame *)malloc(sizeof(strGame));
	memset(tempGame, '\0', sizeof(strGame));
	tempGame->next = NULL;
	tempGame->no = no;
	tempGame->count = count;
	tempGame->answer = (char*)malloc(LIMIT);
	memset(tempGame->answer, '\0', LIMIT);
	strcpy(tempGame->answer, answer);
	tempGame->startTime = (char*)malloc(LIMIT);
	memset(tempGame->startTime, '\0', LIMIT);
	strcpy(tempGame->startTime, startTime);
	tempGame->guessTime = (char*)malloc(LIMIT);
	memset(tempGame->guessTime, '\0', LIMIT);
	strcpy(tempGame->guessTime, guessTime);
	if (guess != NULL){
		for (int i = 0; i < count; i++) {
			tempGame->guess[i] = *(guess+i);
		}
	}
	if (m_gameHead == NULL){
		m_gameHead = tempGame;
		m_gameCurr = m_gameHead;
	}else{
		m_gameCurr->next = tempGame;
		m_gameCurr = tempGame;
	}
	return tempGame;
}

- (strSummary)getSummary{
	return m_summary;
}
- (void)setSummaryCount:(int)count{
	m_summary.count = count;
}

- (strGame *)getGameHead{
	return m_gameHead;
}
- (void)plusGameCount{
	m_gameCount++;
}
- (void)plusGuessCount{
	m_guessCount++;
}
- (void)plusSummaryCount{
	m_summary.count++;
}
- (bool)isNewRecord{
	return m_newRecord;
}
- (void)setNewRecordFalse{
	m_newRecord = false;
}

- (void)destroy1{
	if (m_answer != NULL) {
		free(m_answer);
		m_answer = NULL;
	}
	if (m_playTimeText != NULL){
		free(m_playTimeText);
		m_playTimeText = NULL;
	}
}	
- (void)destroy{
	[super dealloc];
	[self destroy1];
	m_gameCurr = m_gameHead;
	while (m_gameCurr != NULL) {
		free(m_gameCurr->answer);
		free(m_gameCurr->startTime);
		free(m_gameCurr->guessTime);
		strGame *game = m_gameCurr;
		m_gameCurr = m_gameCurr->next;
		game->next = NULL;
		game = NULL;
	}
}

@end
